import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 查询用户表userinfo中的所有数据
 * SELECT id,username,password,nickname,age FROM userinfo
 */
public class JDBCDemo5 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();){
            //1.创建执行对象
            Statement statement = connection.createStatement();
            //2.执行SQL语句:executeQuery("SQL语句")
            String sql = "SELECT id,username,password,nickname,age FROM userinfo";
            ResultSet resultSet = statement.executeQuery(sql);
            /*
                ResultSet对象:获取查询语句的执行结果集;
                需要遍历,一条一条数据获取;
                重要方法: resultSet.next()
                返回值:true : 如果存在下一个元素,返回true
                      false: 如果不存在下一个元素,返回false
             */
            while (resultSet.next()){
                // resultSet.getInt("查询字段名"): 获取结果集数据
                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                String nickname = resultSet.getString("nickname");
                int age = resultSet.getInt("age");
                System.out.println(id+":"+username+":"+password+":"+nickname+":"+age);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}










